package com.hdugraduate;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainMenu extends Activity {
	Button buttonCameraOnMainMenu;
	Button buttonVUOnMainMenu;
	Button buttonSearchOnMainMenu;

	
	OnClickListener listenerCameraOnMainMenu;
	OnClickListener listenerVUOnMainMenu;
	OnClickListener listenerSearchMainMenu;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /* define the button jump to start camera */
        listenerCameraOnMainMenu = new OnClickListener(){
        	public void onClick(View v){
        		Intent intentCameraOnMainMenu = new Intent(MainMenu.this, StartCamera.class);
        		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        			startActivity(intentCameraOnMainMenu);
        		else Toast.makeText(MainMenu.this,R.string.noSDcard, Toast.LENGTH_SHORT).show();
        	}
        };
        
        /* define the button jump to view photos and select to upload */
        listenerVUOnMainMenu = new OnClickListener(){
        	public void onClick(View v){
        		Intent intentVUOnMainMenu = new Intent(MainMenu.this, ViewAndUpload.class);
        		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        			startActivity(intentVUOnMainMenu);
        		else Toast.makeText(MainMenu.this, R.string.noSDcard, Toast.LENGTH_SHORT).show();
        	}
        };
        
        /* define the button jump in Search menu */
        listenerSearchMainMenu = new OnClickListener(){
        	public void onClick(View v){
        		Intent intentSearchOnMainMenu = new Intent(MainMenu.this, SearchAround.class);
        		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        			startActivity(intentSearchOnMainMenu);
        		else Toast.makeText(MainMenu.this, R.string.noSDcard, Toast.LENGTH_SHORT).show();
        	}
        };
        setContentView(R.layout.main);
        buttonCameraOnMainMenu = (Button)findViewById(R.id.mainMenuCamera);
        buttonCameraOnMainMenu.setOnClickListener(listenerCameraOnMainMenu);
        buttonVUOnMainMenu = (Button)findViewById(R.id.mainMenuVU);
        buttonVUOnMainMenu.setOnClickListener(listenerVUOnMainMenu);
        buttonSearchOnMainMenu = (Button)findViewById(R.id.mainMenuSearch);
        buttonSearchOnMainMenu.setOnClickListener(listenerSearchMainMenu);
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
	        File dirFile  = null ;
	        dirFile  =   new  File("/sdcard/TN/");
	        if(!(dirFile.exists()) && !(dirFile.isDirectory())){
	        	dirFile.mkdirs();
	        }
	        dirFile  =   new  File("/sdcard/TN/Photos/");
	        if(!(dirFile.exists()) && !(dirFile.isDirectory())){
	        	dirFile.mkdirs();
	        }
	        dirFile  =   new  File("/sdcard/TN/DB/");
	        if(!(dirFile.exists()) && !(dirFile.isDirectory())){
	        	dirFile.mkdirs();
	        }
        }
        else Toast.makeText(MainMenu.this, R.string.noSDcard, Toast.LENGTH_SHORT).show();
    }
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(Menu.NONE,0,0,"Setting");
		menu.add(Menu.NONE,1,1,"About");
		menu.add(Menu.NONE,2,2,"Quit");
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	switch(item.getItemId()){
    		case 0:
    			Intent menuIntentSettingOnMainMenu = new Intent(MainMenu.this,Setting.class);
    			if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    				startActivity(menuIntentSettingOnMainMenu);
    			else Toast.makeText(MainMenu.this, R.string.noSDcard, Toast.LENGTH_SHORT).show();
    			break;
    		case 1:
    			Intent menuIntentAboutOnMainMenu = new Intent(MainMenu.this,About.class);
    			if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    				startActivity(menuIntentAboutOnMainMenu);
    			else Toast.makeText(MainMenu.this, R.string.noSDcard, Toast.LENGTH_SHORT).show();
    			break;
    		case 2:
    			finish();
    			break;
    	}
    	return true;
    }
}